﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService

Partial Class Control_Subject_Subject
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            'btnDelete.Attributes.Add("onclick", "return ComfirmDialog('Bạn có chắc chắn muốn xóa chủ đề này không?', 0,'" + btnDelete.ClientID + "','0',1);")
            btnSelect.Attributes.Add("onclick", "javacript:setSubjectParentValues()")
            BindToGrid()
            btnDelete.Visible = HasPermission(Function_Name.Subject, Session("RoleID"), 0, Audit_Type.Delete)
            btnEdit.Visible = HasPermission(Function_Name.Subject, Session("RoleID"), 0, Audit_Type.Edit)
        End If
    End Sub

    Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Using Data As New VSEntities
            Dim i As Integer
            Dim intCount As Integer = 0
            Dim intTotal As Integer
            For i = 0 To lbxSubject.Items.Count - 1
                If lbxSubject.Items(i).Selected Then
                    intTotal += 1
                    Dim iSubjectId As Integer = lbxSubject.Items(i).Value
                    Dim q = (From p In Data.Subjects Where p.SubjectId = iSubjectId Select p).FirstOrDefault
                    Dim sTitle As String = q.Subject1
                    Dim sQueryString As String = q.QueryString
                    Try
                        Data.Subjects.DeleteObject(q)
                        Data.SaveChanges()
                        intCount += 1
                        RemoveKeyWordJavascript(sTitle)
                        RemoveKeyWordJavascript(sQueryString)
                        Insert_App_Log("Edit KeyWord:" & sTitle & "", Function_Name.Subject, Audit_Type.Delete, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                    Catch ex As Exception
                        log4net.Config.XmlConfigurator.Configure()
                        log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                    End Try
                End If
            Next
            If intCount > 0 Then
                Excute_Javascript("Alertbox('Xóa dữ liệu thành công. " & intCount.ToString & " /" & intTotal.ToString & " record.');", Me.Page, True)
                BindToGrid()
            Else
                Excute_Javascript("Alertbox('Xóa thất bại.');", Me.Page, True)
            End If
        End Using
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        BindToGrid(txtSubject.Text.Trim)
    End Sub

    Private Sub BindToGrid(Optional ByVal strFilter As String = "")
        Using Data As New VSEntities
            Dim dt = (From q In Data.Subjects Where q.Subject1.Contains(strFilter) Order By q.Subject1 Ascending Select q)
            With lbxSubject
                .DataSource = dt
                .DataTextField = "Subject1"
                .DataValueField = "SubjectId"
                .DataBind()
            End With
            txtSubject.Text = ""
        End Using
    End Sub

    Protected Sub btnEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        Using Data As New VSEntities
            Dim i As Integer
            Dim intId As Integer = 0
            For i = 0 To lbxSubject.Items.Count - 1
                If lbxSubject.Items(i).Selected Then
                    intId = lbxSubject.Items(i).Value
                End If
            Next
            If intId > 0 Then
                Response.Redirect("Edit.aspx?id=" & intId)
            End If
        End Using
    End Sub
End Class
